<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="心理师级别" prop="levelInfo">
        <el-select v-model="queryParams.levelInfo" placeholder="请选择心理师级别" clearable>
          <el-option
            v-for="dict in dict.type.advisor_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接单状态" prop="workType">
        <el-select v-model="queryParams.workType" placeholder="请选择接单状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否公益" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择是否公益" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="isFlag">
        <el-select v-model="queryParams.isFlag" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.check_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:resume:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:resume:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:resume:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleApprove"
          v-hasPermi="['system:resume:add']"
        >审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:resume:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resumeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="" align="center" prop="id" v-if="true"/>
      <el-table-column label="咨询师id" align="center" prop="userId"/>
      <el-table-column label="个人简介" align="center" prop="descInfo"/>
      <el-table-column label="擅长领域" align="center" prop="goodDesc"/>
      <el-table-column label="工作年限" align="center" prop="workTime"/>
      <el-table-column label="擅长疗法" align="center" prop="goodTherapy"/>
      <el-table-column label="培训经历" align="center" prop="trainingExperience"/>
      <el-table-column label="心理师级别" align="center" prop="levelInfo">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.advisor_type" :value="scope.row.levelInfo"/>
        </template>
      </el-table-column>
      <el-table-column label="用户满意度" align="center" prop="agreement"/>
      <el-table-column label="帮助人数" align="center" prop="helpPeople"/>
      <el-table-column label="接单状态" align="center" prop="workType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.workType"/>
        </template>
      </el-table-column>
      <el-table-column label="平台保障" align="center" prop="platformGuarantee"/>
      <el-table-column label="是否公益" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="公益时长" align="center" prop="freeTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:resume:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:resume:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改咨询师个人简历对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :disabled="disabled"  label-width="80px">
        <el-form-item label="咨询师id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入咨询师id"/>
        </el-form-item>
        <el-form-item label="个人简介" prop="descInfo">
          <el-input v-model="form.descInfo" placeholder="请输入个人简介"/>
        </el-form-item>
        <el-form-item label="擅长领域" prop="goodDesc">
          <el-input v-model="form.goodDesc" placeholder="请输入擅长领域"/>
        </el-form-item>
        <el-form-item label="工作年限" prop="workTime">
          <el-input v-model="form.workTime" placeholder="请输入工作年限"/>
        </el-form-item>
        <el-form-item label="擅长疗法" prop="goodTherapy">
          <el-input v-model="form.goodTherapy" placeholder="请输入擅长疗法"/>
        </el-form-item>
        <el-form-item label="培训经历" prop="trainingExperience">
          <el-input v-model="form.trainingExperience" placeholder="请输入培训经历"/>
        </el-form-item>
        <el-form-item label="心理师级别" prop="levelInfo">
          <el-select v-model="form.levelInfo" placeholder="请选择心理师级别">
            <el-option
              v-for="dict in dict.type.advisor_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户满意度" prop="agreement">
          <el-input v-model="form.agreement" placeholder="请输入用户满意度"/>
        </el-form-item>
        <el-form-item label="帮助人数" prop="helpPeople">
          <el-input v-model="form.helpPeople" placeholder="请输入帮助人数"/>
        </el-form-item>
        <el-form-item label="接单状态" prop="workType">
          <el-select v-model="form.workType" placeholder="请选择接单状态">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平台保障" prop="platformGuarantee">
          <el-input v-model="form.platformGuarantee" placeholder="请输入平台保障"/>
        </el-form-item>
        <el-form-item label="是否公益">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="公益时长" prop="freeTime">
          <el-input v-model="form.freeTime" placeholder="请输入公益时长"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!disabled" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button v-else :loading="buttonLoading" type="primary" @click="approve">通 过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listResume, getResume, delResume, addResume, updateResume, approveResume} from "@/api/system/resume";

export default {
  name: "Resume",
  dicts: ['sys_yes_no', 'advisor_type', 'check_flag'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 咨询师个人简历表格数据
      resumeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        descInfo: undefined,
        goodDesc: undefined,
        workTime: undefined,
        goodTherapy: undefined,
        trainingExperience: undefined,
        levelInfo: undefined,
        agreement: undefined,
        helpPeople: undefined,
        workType: undefined,
        platformGuarantee: undefined,
        status: undefined,
        freeTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "咨询师id不能为空", trigger: "blur"}
        ],
        descInfo: [
          {required: true, message: "个人简介不能为空", trigger: "blur"}
        ],
        goodDesc: [
          {required: true, message: "擅长领域不能为空", trigger: "blur"}
        ],
        workTime: [
          {required: true, message: "工作年限不能为空", trigger: "blur"}
        ],
        goodTherapy: [
          {required: true, message: "擅长疗法不能为空", trigger: "blur"}
        ],
        trainingExperience: [
          {required: true, message: "培训经历不能为空", trigger: "blur"}
        ],
        levelInfo: [
          {required: true, message: "心理师级别不能为空", trigger: "change"}
        ],
        agreement: [
          {required: true, message: "用户满意度不能为空", trigger: "blur"}
        ],
        helpPeople: [
          {required: true, message: "帮助人数不能为空", trigger: "blur"}
        ],
        workType: [
          {required: true, message: "接单状态 0 否 1 是不能为空", trigger: "change"}
        ],
        platformGuarantee: [
          {required: true, message: "平台保障不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "是否公益 0 否 1 是不能为空", trigger: "blur"}
        ],
        freeTime: [
          {required: true, message: "公益时长不能为空", trigger: "blur"}
        ]
      },

      disabled: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询咨询师个人简历列表 */
    getList() {
      this.loading = true;
      listResume(this.queryParams).then(response => {
        this.resumeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.disabled = !this.disabled
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        descInfo: undefined,
        goodDesc: undefined,
        workTime: undefined,
        goodTherapy: undefined,
        trainingExperience: undefined,
        levelInfo: undefined,
        agreement: undefined,
        helpPeople: undefined,
        workType: undefined,
        platformGuarantee: undefined,
        status: "0",
        freeTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加咨询师个人简历";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getResume(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改咨询师个人简历";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateResume(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addResume(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },

    /**
     * 审核通过
     */
    approve() {
      let data = {
        userId: this.form.userId,
        isFlag: '1'
      }

      approveResume(data).then(response => {
        this.buttonLoading = true;
        this.$modal.msgSuccess("审核成功");
        this.open = false;
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除咨询师个人简历编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delResume(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },

    /** 审核操作 */
    handleApprove(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getResume(id).then(response => {
        this.loading = false;
        this.disabled = true
        this.form = response.data;
        this.open = true;
        this.title = "审核咨询师个人简历";
      });

    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/resume/export', {
        ...this.queryParams
      }, `resume_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
